package com.wjwframework.jfinal.web.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.wjw.utils.GUID;
import com.wjw.utils.RegExpUtil;
import com.wjwframework.jfinal.model.BaseModelImpl;
import com.wjwframework.jfinal.model.CommonDbUtil;
import com.wjwframework.jfinal.vo.PageNavation;
import com.wjwframework.jfinal.web.model.base.BaseSysRole;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class SysRole extends BaseSysRole<SysRole> implements BaseModelImpl {
	public static final SysRole dao = new SysRole();
	
	@Override
	public CommonDbUtil getCommDbUtil() {
		return new CommonDbUtil(getClass());
	}	
	
	@Override
	public boolean deleteByIds(String ids) {
		String sql = "DELETE FROM "+getCommDbUtil().tableName+" WHERE id in("+ids+")";
		return Db.update(sql)>0;
	}

	@Override
	public PageNavation findList(PageNavation page) {
		String sql="FROM sys_role ";
		sql+=getCommDbUtil().getLikeOrSortSql(page,true,"coding","role_name");
		Page<SysRole> objPage=dao.paginate(page.getPageNow(),page.getPageSize(),
				"SELECT *",sql);
		page.getParamMap().put("list", objPage.getList());
		page.setTotalRow(objPage.getTotalRow());
		page.setTotalPage(objPage.getTotalRow()/page.getPageSize()+(objPage.getTotalRow()%objPage.getPageSize()>0?1:0));//总页码
		return page;
	}
	
	/**
	 * 获取 所有role对象的集合
	 */
	public List<SysRole> findAllList(){
		String sql="select * from sys_role";
		List<SysRole> list=dao.find(sql);
		return list;
	}
	
	/**
	 * 检测角色编码coding是否存在
	 */
	public boolean isExistCoding(Map<String,Object> paramMap){
		String sql="select count(id) from sys_role where coding=? ";
		if(paramMap.get("id")!=null){
    		sql+=" AND id!='"+paramMap.get("id").toString()+"'";
    	}
		Long count=Db.queryLong(sql,paramMap.get("coding"));
    	return count>0;
	}
	
	/**
	 * 检测批量角色编码coding是否存在
	 * 不存在返回list为空
	 */
	public List<Record> isExistCodings(String codings){
		String sql="SELECT coding FROM sys_role WHERE coding IN ("+codings+")";
		List<Record> reList=Db.find(sql);
    	return reList;
	}
	
	/**
	 * 检测角色是否有用户关联信息
	 */
	public boolean isExistUserRelate(String ids) {
		String sql = "SELECT COUNT(*) FROM sys_user WHERE id IN("+ids+")";
		return Db.queryLong(sql)>0;
	}
	
    /**
     * 获取下一个seq排序序号
     */
    public Long getNextSeq() {
    	String sql="SELECT MAX(seq)+1 FROM sys_role ";
    	Long seq=Db.queryLong(sql);
    	return seq==null?1:seq;
    }
    
    /**
     * 通过角色编码获取角色id
     */
    public String getIdBycoding(String coding) {
    	String sql="SELECT id FROM sys_role where coding=?";
    	String id=Db.queryStr(sql,coding);
    	return id;
    }
    
    //将JSON数组转换为对象数组
  	public List<Record> parseJson(JSONArray json) {
  		List<Record> list=new ArrayList<Record>();
      	if(json!=null){
      		for(int i=0;i<json.size();i++){
      			JSONObject obj=json.getJSONObject(i);
      			Record item=new Record();
      			item.set("id",GUID.create());
      			item.set("index", obj.getInteger("index"));
      			item.set("role_name", obj.getString("role_name"));
      			item.set("coding", obj.getString("coding"));
      			String seq=obj.getString("seq");
      			if(StringUtils.isNotBlank(seq)){
      				if(!RegExpUtil.isNumeric(seq)){
      					continue;
      				}else{
      					item.set("seq", Long.parseLong(seq));
      				}
      			}else{
      				item.set("seq", getNextSeq());
      			}
      			list.add(item);
      		}
      	}
  		return list;
  	}
}
